<?php
namespace Home\Controller;
use Think\Controller;
class DropdownController extends Controller {
	
	public function __construct() {
		parent::__construct();
		$ti = M('ti') -> where("user_id = '".$_SESSION['user']['id']."'") -> find();
		
		if(empty($ti) || empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
	/*public function __construct() {
		parent::__construct();
		if(empty($_SESSION['visitor_id']))
		{
			header('Location: http://container.open.taobao.com/container?appkey=23344775');
		}
    }*/
	
	
	public function index(){
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$type=empty($_REQUEST['type'])?'':trim($_REQUEST['type']);
		$where="type = '$type'";
		$visitor_id = $_SESSION['user']['id'];
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		
		$drop=M('drop_down');
		$info=$drop->field('id,name,type')->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->order('id desc')->select();
		$count['count']=$drop->where($where)->count();
		$count['pageNo']=$pageNo;
		$count['pageSize']=$pageSize;
		$count['countpage']=ceil($count['count']/$count['pageSize']);
		$order['name']='id';
		$order['asc']='desc';
		
		$this->assign('type',$type);
		$this->assign('listtitle',$this->get_listtitle($type));
		$this->assign('info',$info);
		$this->assign("order",$order);
		$this->assign("count",$count);	
		$this->display();
    }
	
	public function dropdown_find($type,$pageNo,$pageSize,$title,$name,$asc){
		$type=empty($_REQUEST['type'])?'':trim($_REQUEST['type']);
		$where="type = '$type'";
		$visitor_id = $_SESSION['user']['id'];
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		if(!empty($title)){
			$title=trim($title);
			$where['name'] = array('like',"%$title%");
			$data['title']=$title;
		}
		
		if(empty($name)){
			$name='id';
		}
		if(empty($asc)){
			$asc='desc';
		}
		
		$drop = M("drop_down"); 
		$count['count']=$drop->where($where)->count();
		$count['countpage']=ceil($count['count']/$pageSize);
		
		if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		}
		if($pageNo<=0){
			$pageNo=1;
		}
		
		$info=$drop->where($where)->order("$name $asc")->limit(($pageNo-1)*$pageSize,$pageSize)->select(); 
		
		$count['pageSize']=$pageSize;
		$count['pageNo']=$pageNo;
		$count['num']=$count['countpage']-4;
		$order['name']=$name;
		$order['asc']=$asc;
		$this->assign("info",$info);
		$this->assign("type",$type);
		$this->assign('listtitle',$this->get_listtitle($type));
		$this->assign("data",$data);
		$this->assign("res",$res);
		$this->assign("count",$count);
		$this->display('index'); 
	}
	
	public function dropdown_delete(){
		$visitor_id = $_SESSION['user']['id'];  
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		$where = "id='$id'";
		$visitor_id = $_SESSION['user']['id'];
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		$drop=M('drop_down');
		$res=$drop->where($where)->delete();
		 
		header("Location:".$_SERVER['HTTP_REFERER']);
    }
	
	public function dropdown_info(){
		//var_dump($_REQUEST);die();
		$name=empty($_REQUEST['name'])?'':trim($_REQUEST['name']);
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$type=empty($_REQUEST['type'])?'':$_REQUEST['type'];
		
		if($id){
			$this->dropdown_edit($name,$id);
		}else{
			$this->dropdown_add($name,$type);
		}
    }
	
	public function dropdown_add($name,$type){
		
		
		if(!empty($name))
		{
		    $drop=M('drop_down');
			$data['name']=$name;
			$data['type']=$type;
			$data['add_time']=date('Y-m-d H:i:s');
			$data['modify_time']=date('Y-m-d H:i:s');
			$data['visitor_id']	 = $_SESSION['user']['id'];
			$res=$drop->add($data);
			
			$this->redirect('index', array('type' => $type));
		}
    }
	
	public function dropdown_edit($name,$id){
		$drop=M('drop_down');
		
		if(!empty($id))
		{
			$data['name']=$name;
			$data['modify_time']=date('Y-m-d H:i:s') ;
			$visitor_id=$_SESSION['user']['id'];
			$where = "id='$id'";
			$visitor_id = $_SESSION['user']['id'];
			$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
			if($shouquan['group_id']){
				$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
				foreach($shouquan2 as $k=> $val){
					if($k == '0'){
						$s_str = "'".$val['visitor_id']."'";
					}else{
						$s_str .= ","."'".$val['visitor_id']."'";
					}
				}
				$where.= " and visitor_id in($s_str)";
			}else{
				$where .= " and visitor_id = '$visitor_id'";
			}
			
			$res=$drop->where($where)->save($data);
			if($res!==false)
			{
				$this->success('修改成功');
			}
			else
			{
				$this->success('修改失败');
			}
		}
    }
	
	/*  双击修改  */
	public function dropdown_click_edit(){
		
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$value=empty($_REQUEST['value'])?'':$_REQUEST['value'];
		
		$drop=M('drop_down');
		
		if(!empty($id))
		{
			$data['name']=$value;
			$data['modify_time']=date('Y-m-d H:i:s');
			$where = "id='$id'";
			$visitor_id = $_SESSION['user']['id'];
			$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
			if($shouquan['group_id']){
				$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
				foreach($shouquan2 as $k=> $val){
					if($k == '0'){
						$s_str = "'".$val['visitor_id']."'";
					}else{
						$s_str .= ","."'".$val['visitor_id']."'";
					}
				}
				$where.= " and visitor_id in($s_str)";
			}else{
				$where .= " and visitor_id = '$visitor_id'";
			}
			
			$res=$drop->where($where)->save($data);
			if($res!==false)
			{
				$msg='success';
			}
			else
			{
				$msg='fail';
			}
		
		   echo $msg;
		}
    }
	
	function get_listtitle($type)
	{
		if($type=='pay_method')
		{
			$title='支付方式';
		}
		elseif($type=='customer_group')
		{
			$title='客户组管理';
		}
		elseif($type=='payee')
		{
			$title='收款人管理';
		}
		elseif($type=='purpose')
		{
			$title='支出用途';
		}
		elseif($type=='service')
		{
			$title='客服管理';
		}
		
		return $title;
	}
	
}
















